package com.vcc.boss.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.vcc.core.annotations.RestPack;
import com.vcc.core.exception.NotFoundException;
import com.vcc.core.exception.ProgramException;
import com.vcc.data.entity.BossRole;
import com.vcc.data.entity.CardType;
import com.vcc.data.entity.UserCardWallet;
import com.vcc.service.boss.request.userCard.BatchUserCardWalletDto;
import com.vcc.service.boss.request.userCard.UserCardWalletDto;
import com.vcc.service.boss.response.userCard.UserCardWalletVoBoss;
import com.vcc.service.sa.BossUserUtil;
import com.vcc.service.service.AdminParamService;
import com.vcc.service.service.CardTypeService;
import com.vcc.service.service.UserCardWalletService;
import com.vcc.service.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("bossUserCardWallet")
public class UserCardWalletController {

    @Resource
    private UserCardWalletService userCardWalletService;

    @Resource
    private CardTypeService cardTypeService;

    @Resource
    private UserService userService;
    @Autowired
    private AdminParamService adminParamService;

    @SaCheckPermission(value = "client:user:card:wallet:view")
    @RestPack
    @SaCheckLogin
    @GetMapping("listUserCardWallet")
    public List<UserCardWalletVoBoss> listUserCardWallet(
            Long targetUserId
    ) {
        if (targetUserId == null) {
            throw new ProgramException("用户ID不能为空");
        }
        Long dealerId = userService.getDealerIdById(targetUserId);
        if (!BossUserUtil.hasAllData() && !dealerId.equals(BossUserUtil.getId())) {
            throw new ProgramException("无权查看该用户的参数配置");
        }
        List<CardType> list = cardTypeService.list();
        List<UserCardWallet> userCardWallets = userCardWalletService.listByUserId(targetUserId,
                list.stream().filter(it -> it.getStatus() == 0).map(CardType::getId).collect(Collectors.toList()));
        return userCardWallets
                .stream()
                .map(it ->
                        new UserCardWalletVoBoss(
                                list.stream()
                                        .filter(card -> card.getId().equals(it.getCardId()))
                                        .findFirst()
                                        .orElseThrow(() -> new NotFoundException("没有当前卡信息")),
                                it)
                ).collect(Collectors.toList());
    }

    @SaCheckPermission(value = "client:user:card:wallet:edit")
    @RestPack
    @SaCheckLogin
    @PostMapping("updateUserCardWallet")
    public Boolean updateUserCardWallet(
            @RequestBody UserCardWalletDto userCardWalletDto
    ) {
        UserCardWallet userCardWallet = userCardWalletService.getById(userCardWalletDto.getId());
        Long dealerId = userService.getDealerIdById(userCardWallet.getUserId());
        if (!BossUserUtil.hasAllData() && !dealerId.equals(BossUserUtil.getId())) {
            throw new ProgramException("无权操作该用户的参数配置");
        }
        //判断开卡费小于-1，则报错
        BossRole role = BossUserUtil.getRole();
        BigDecimal minAmount = BigDecimal.valueOf(-1);
        BigDecimal minRechargeRate = BigDecimal.ZERO;
        if (role.getHasParamStatus()) {
            String openCardMin = adminParamService.getValueByCode("SET_USER_CARD_WALLET_AMOUNT_MIN_AMOUNT", "-1");
            String rechargeMingRate = adminParamService.getValueByCode("SET_USER_CARD_WALLET_RECHARGE_MING_RATE", "0");
            minAmount = new BigDecimal(openCardMin);
            minRechargeRate = new BigDecimal(rechargeMingRate);
        }
        if (userCardWallet.getAmount().compareTo(userCardWalletDto.getAmount()) != 0 && userCardWalletDto.getAmount().compareTo(minAmount) < 0) {
            throw new ProgramException("开卡费调整失败，金额不能小于" + minAmount);
        }
        if (userCardWallet.getExchangeRate().compareTo(userCardWalletDto.getExchangeRate()) != 0 && userCardWalletDto.getExchangeRate().compareTo(minRechargeRate) < 0) {
            throw new ProgramException("汇率调整失败，金额不能小于" + minRechargeRate);
        }
        if (StpUtil.hasPermission("client:user:transaction-rate:edit")) {
            if (userCardWalletDto.getOpenCardOnLineCount() < 1) {
                throw new ProgramException("单次开卡上线调整失败，次数不能小于1");
            }
            if (userCardWalletDto.getAuthFee().compareTo(BigDecimal.ZERO) < 0) {
                throw new ProgramException("认证费调整失败，金额不能小于0");
            }
            if (userCardWalletDto.getAuthFeeRate().compareTo(BigDecimal.ZERO) < 0) {
                throw new ProgramException("认证费率调整失败，金额不能小于0");
            }
            if (userCardWalletDto.getFailFee().compareTo(BigDecimal.ZERO) < 0) {
                throw new ProgramException("失败费调整失败，金额不能小于0");
            }
            if (userCardWalletDto.getFailFeeRate().compareTo(BigDecimal.ZERO) < 0) {
                throw new ProgramException("失败费率调整失败，金额不能小于0");
            }
            if (userCardWalletDto.getMinAuthFee().compareTo(BigDecimal.ZERO) < 0) {
                throw new ProgramException("最小认证费调整失败，金额不能小于0");
            }
            if (userCardWalletDto.getMinAuthFeeRate().compareTo(BigDecimal.ZERO) < 0) {
                throw new ProgramException("最小认证费率调整失败，金额不能小于0");
            }
            if (userCardWalletDto.getReversalFee().compareTo(BigDecimal.ZERO) < 0) {
                throw new ProgramException("撤销费调整失败，金额不能小于0");
            }
            if (userCardWalletDto.getReversalFeeRate().compareTo(BigDecimal.ZERO) < 0) {
                throw new ProgramException("撤销费率调整失败，金额不能小于0");
            }
            if (userCardWalletDto.getRefundFee().compareTo(BigDecimal.ZERO) < 0) {
                throw new ProgramException("退款费调整失败，金额不能小于0");
            }
            if (userCardWalletDto.getRefundFeeRate().compareTo(BigDecimal.ZERO) < 0) {
                throw new ProgramException("退款费率调整失败，金额不能小于0");
            }
            if (userCardWalletDto.getCrossBorderFee().compareTo(BigDecimal.ZERO) < 0) {
                throw new ProgramException("跨境费调整失败，金额不能小于0");
            }
            if (userCardWalletDto.getCrossBorderFeeRate().compareTo(BigDecimal.ZERO) < 0) {
                throw new ProgramException("跨境费率调整失败，金额不能小于0");
            }
        } else {
            userCardWalletDto.setAuthFee(null);
            userCardWalletDto.setAuthFeeRate(null);
            userCardWalletDto.setFailFee(null);
            userCardWalletDto.setFailFeeRate(null);
            userCardWalletDto.setMinAuthFee(null);
            userCardWalletDto.setMinAuthFeeRate(null);
            userCardWalletDto.setReversalFee(null);
            userCardWalletDto.setReversalFeeRate(null);
            userCardWalletDto.setRefundFee(null);
            userCardWalletDto.setRefundFeeRate(null);
            userCardWalletDto.setCrossBorderFee(null);
            userCardWalletDto.setCrossBorderFeeRate(null);
        }
        return userCardWalletService.updateInfo(userCardWalletDto);
    }


    @SaCheckPermission(value = "client:user:card:wallet:batch:edit")
    @RestPack
    @SaCheckLogin
    @PostMapping("batchUpdateUserCardWallet")
    public Boolean batchUpdateUserCardWallet(
            @RequestBody BatchUserCardWalletDto batchUserCardWalletDto
    ) {
        return userCardWalletService.batchUpdateInfo(batchUserCardWalletDto);
    }
}
